package com.kkkkkba.search.dataSource;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kkkkkba.search.common.ErrorCode;
import com.kkkkkba.search.exception.BusinessException;
import com.kkkkkba.search.model.entity.Music;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 音乐服务实现类
 */
@Service
public class MusicDataSource implements DataSource<Music> {

    public static final String MUSIC_URL_1 = "https://www.fangpi.net/s/%s";
    public static final String MUSIC_URL_2 = "https://www.gequbao.com/s/%s";
    @Override
    public Page<Music> doSearch(String searchText, long pageNum, long pageSize) {
        long current = (pageNum - 1) * pageSize + 1;
        if (current == 1) {
            pageSize = 5;
            current = 10;
        }
        if (searchText == null || searchText.equals("")) {
            searchText = "假如有一天我变得很有钱";
        }

        String url = String.format(
                MUSIC_URL_2,
                searchText);
        Document doc = null;
        Elements newsHeadlines = null;
        List<Music> musicList = new ArrayList<>();
        try {
            doc = Jsoup.connect(url).get();
            newsHeadlines = doc.select(".text-primary.font-weight-bold");
            Elements singers = doc.select(".text-success.col-4.col-content");
            // 外链
            for (int i = 0; i < newsHeadlines.size(); i++) {
                Element headline = newsHeadlines.get(i);
                Element singer = singers.get(i);
                String href = headline.attr("href");
//                System.out.println(href);
                Music music = new Music(headline.text(),singer.text(), href);
                musicList.add(music);

            }

        } catch (IOException e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "数据获取异常");
        }
//        musicList.forEach(System.out::println);
        Page<Music> musicPage = new Page<>(pageNum,pageSize);
        musicPage.setRecords(musicList);
        return musicPage;
    }
}


/**
 * 子页面爬虫（超时）
 */
/**
            List<Thread> threadList = new ArrayList<>();
            for (int i = start; i < end; i++) {
                Element headline = newsHeadlines.get(i);
                Thread t =  new Thread(() -> {
                    String href = subpageUrl + headline.attr("href");
                    Document document = null;
                    try {
                        document = Jsoup.connect(href).get();
                    } catch (IOException e) {
                        throw new BusinessException(ErrorCode.SYSTEM_ERROR, "数据获取异常");
                    }
                    Element script = document.select("script").get(4);

                    int urlStart = script.toString().indexOf("https:");
                    int urlEnd = script.toString().indexOf(".replace");
//                    System.out.println(urlStart+ " "+ urlEnd);

                    if(!(urlEnd == -1  || urlStart == -1)){
                        String url3  = script.toString().substring(urlStart,urlEnd-1);
//                        System.out.println(url3);
                        Music music = new Music(headline.text(), url3);
                        musicList.add(music);
                    }
                });
                t.start();
                threadList.add(t);
            }

            // 等待所有线程执行结束
            for (Thread t : threadList) {
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
 */